package it.polimi.comorazio.fragments;

import it.polimi.comorazio.R;
import it.polimi.comorazio.activities.PointOfInterestDetailActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ImageFragment extends Fragment {

	public static final String ARG_POSITION = "position";
	public static final String ARG_RESOURCE_ID = "resources_id";

	private int imageResourceId;
	private String position;

	public ImageFragment() {
		this.imageResourceId = -1;
		this.position = null;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (getArguments().containsKey(ARG_POSITION)) {
			this.position = getArguments().getString(ARG_POSITION);
		}

		if (getArguments().containsKey(ARG_RESOURCE_ID)) {
			this.imageResourceId = getArguments().getInt(ARG_RESOURCE_ID);
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.image_layout, container, false);

		if (this.imageResourceId != -1) {
			ImageView imageView = (ImageView) view
					.findViewById(R.id.imageView1);
			imageView.setImageResource(imageResourceId);
			if (position != null) {
				imageView
						.setBackgroundResource(R.drawable.selector_yellow_transparent);
				imageView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Log.i(this.getClass().getSimpleName(),
								"clicked image [" + position + "]");
						Intent detailIntent = new Intent(getActivity(),
								PointOfInterestDetailActivity.class);
						detailIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
								| Intent.FLAG_ACTIVITY_NEW_TASK);

						detailIntent.putExtra(
								PointOfInterestDetailFragment.ARG_ITEM_ID,
								position);

						startActivity(detailIntent);
						getActivity().finish();
					}
				});
			}
		}

		return view;
	}
}